var express = require('express');
var router = express.Router();
var query = require("../db/db").query;
var queryPromise = require("../db/db").queryPromise;
var query_cat = require("../db/db").query_cat
/* GET users listing. */
router.get('/getmenu', async function (req, res, next) {
  console.log("传过来的登陆id为——————", req.session.user_id)
  if (req.session.user_id) {
    try {
      // res.send('respond with a resource');
      //获取用户对应的菜单项  去后台获取菜单数据
      let user_id = req.session.user_id
      let sql = "select * from user_role where user_id = ?"
      let values = [user_id]
      let menus = []
      let {
        results
      } = await queryPromise(sql, values)
      let role_id = results[0].role_id //1\
      let sql_menus = "select * from role_menu where role_id=?"
      let res_menus = await queryPromise(sql_menus, [role_id]); //查询角色拥有的菜单的id
      let res2 = res_menus.results
      /*     let cat_ids = [] //角色里面所有菜单id
          res2.map(v => {
            cat_ids.push(v.cat_id)
          }) */
      console.log(res2)
      for (let i = 0; i < res2.length; i++) {
        let cat_res = await query_cat(res2[i].cat_id)
        menus.push({
          cat_id: cat_res[0].cat_id,
          cat_name: cat_res[0].cat_name,
          cat_pid: cat_res[0].cat_pid,
        })
      }
      res.json({
        code: 1,
        data: menus
      })
    } catch (err) {
      res.json({
        code: -1,
        message: err
      })
    }
  } else {
    res.json({
      code: -1,
      message: "请先登陆！"
    })
  }
});

module.exports = router;